package com.gmss.crawler.module.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.CommonEnum;
import com.gmss.crawler.module.company.domain.Company;
import com.gmss.crawler.module.company.service.ICompanyService;
import com.gmss.crawler.service.PlaceCrawlerService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 单位表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-10-15
 */
@RestController
@RequestMapping("/company")
public class CompanyController extends Base {

    @Resource
    private ICompanyService iCompanyService;
    @Resource
    private PlaceCrawlerService placeCrawlerService;

    /**
     * 通过id查询
     */
    @GetMapping("/getById/{id}")
    public String getById(@PathVariable(value = "id") Long id) {
        return respJson(CommonEnum.SUCCESS, iCompanyService.getById(id));
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    public String save(@RequestBody Company company) {
        iCompanyService.save(company);
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 新增
     */
    @PostMapping("/save/school/info")
    public String saveSchoolInfo() {
        List<Company> list = placeCrawlerService.parseUniversity();
        iCompanyService.saveBatch(list);
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 添加研究所和研究所所在地到数据库
     */
    @PostMapping("/saveGraduateSchool")
    public String saveGranduateSchool(){
        List<Company> companyList = placeCrawlerService.paserData();
        iCompanyService.saveBatch(companyList);
        return  respJson(CommonEnum.SUCCESS);
    }

    /**
     * 通过id删除
     */
    @DeleteMapping("/deleteById/{id}")
    public String delete(@PathVariable(value = "id") String ids) {
        String[] idsStrs = ids.split(",");
        for (String id : idsStrs) {
            iCompanyService.removeById(Long.parseLong(id));
        }
        return respJson(CommonEnum.SUCCESS);
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public String updateById(@RequestBody Company company) {
        iCompanyService.updateById(company);
        return respJson(CommonEnum.SUCCESS);
    }


    /**
     * 查询列表
     */
    @PostMapping("/list")
    public String list(@RequestBody CompanyReqVo company) {
        final LambdaQueryWrapper<Company> lambda = new QueryWrapper<Company>().lambda();
        this.buildCondition(lambda, company);
        return respJson(CommonEnum.SUCCESS, iCompanyService.list(lambda));

    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public String page(@RequestBody CompanyReqVo pageParam) {
        final LambdaQueryWrapper<Company> lambda = new QueryWrapper<Company>().lambda();
        this.buildCondition(lambda, pageParam);
        final IPage<Company> page = iCompanyService.page(new Page<>(Constants.PAGE_NO, Constants.PAGE_SIZE), lambda);
        return respJson(CommonEnum.SUCCESS, page);
    }

    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<Company> lambda, CompanyReqVo param) {
        if (null != param.getId()) {
            lambda.eq(Company::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getCompanyName())) {
            lambda.eq(Company::getCompanyName, param.getCompanyName());
        }
        if (!StringUtils.isEmpty(param.getAddress())) {
            lambda.eq(Company::getAddress, param.getAddress());
        }
        if (!StringUtils.isEmpty(param.getCompanyType())) {
            lambda.eq(Company::getCompanyType, param.getCompanyType());
        }
        if (!StringUtils.isEmpty(param.getShcoolType())) {
            lambda.eq(Company::getShcoolType, param.getShcoolType());
        }
        if (!StringUtils.isEmpty(param.getDepartment())) {
            lambda.eq(Company::getDepartment, param.getDepartment());
        }
        if (!CollectionUtils.isEmpty(param.getCreateTimeList())) {
            lambda.ge(Company::getCreateTime, param.getCreateTimeList().get(0));
            lambda.le(Company::getCreateTime, param.getCreateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            lambda.eq(Company::getCreateUser, param.getCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getUpdateTimeList())) {
            lambda.ge(Company::getUpdateTime, param.getUpdateTimeList().get(0));
            lambda.le(Company::getUpdateTime, param.getUpdateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUpdateUser())) {
            lambda.eq(Company::getUpdateUser, param.getUpdateUser());
        }
        lambda.orderBy(true, false, Company::getId);
    }


    /**
     * 请求model
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class CompanyReqVo extends Company {
        private List<String> createTimeList; // 创建时间起止
        private List<String> updateTimeList; // 修改时间起止
    }


}
